<template>
	<view class="signa" :style="{width:width,height:height}">
		<view class="head">
			<view class="title">电子签名</view>
			<view class="cancel" @click="cancel">取消</view>
		</view>
		<view class="btn">
			<view class="cancel-btn" @click="clear">
				重写
			</view>
			<view class="save-btn" @click="save">
				保存
			</view>
		</view>
		<canvas class="canvas" disable-scroll="true" canvas-id="designature" @touchstart="starts"
		 @touchmove="moves" @touchend="end"></canvas>
	</view>
</template>

<script>
	
	export default {
		props:{
			width: {
				type: String,
				default: ''
			},
			height: {
				type:String,
				default: ''
			}
		},
		data() {
			return {
				resultUrl: '',
				dom: null,
				line: [],
				radius: 0
			}
		},
		created() {
			if(this.width == '' && this.height == ''){
				uni.getSystemInfo({
					success: (res) => {
						this.width = res.windowWidth + 'px';
						this.height = res.windowHeight / 2 + 'px';
					}
				});
			}
			this.dom = uni.createCanvasContext('designature',this);
		},
		methods: {
			end(e) {

			},
			distance(a, b) {
				let x = b.x - a.x;
				let y = b.y - a.y;
				return Math.sqrt(x * x + y * y);
			},
			starts(e) {
				this.line.push({
					points: [{
						time: new Date().getTime(),
						x: e.touches[0].x,
						y: e.touches[0].y,
						dis: 0
					}]
				})
				let currentPoint = {
					x: e.touches[0].x,
					y: e.touches[0].y
				}
				this.currentPoint = currentPoint
				this.drawer(this.line[this.line.length - 1])
			},
			moves(e) {
				let point = {
					x: e.touches[0].x,
					y: e.touches[0].y
				}
				this.lastPoint = this.currentPoint,
					this.currentPoint = point
				this.line[this.line.length - 1].points.push({
					time: new Date().getTime(),
					x: e.touches[0].x,
					y: e.touches[0].y,
					dis: this.distance(this.currentPoint, this.lastPoint)
				})
				this.drawer(this.line[this.line.length - 1])
			},
			drawer(item) {
				let x1, x2, y1, y2, len, radius, r, cx, cy, t = 0.5,
					x, y;
				var time = 0;
				if (item.points.length > 2) {
					let lines = item.points[item.points.length - 3];
					let line = item.points[item.points.length - 2];
					let end = item.points[item.points.length - 1];
					x = line.x;
					y = line.y;
					x1 = lines.x;
					y1 = lines.y;
					x2 = end.x;
					y2 = end.y;
					var dis = 0;
					time = (line.time - lines.time) + (end.time - line.time)
					dis = line.dis + lines.dis + end.dis;
					var dom = this.dom;
					var or = Math.min(time / dis * this.linePressure + this.lineMin, this.lineMax);
					cx = (x - (Math.pow(1 - t, 2) * x1) - Math.pow(t, 2) * x2) / (2 * t * (1 - t))
					cy = (y - (Math.pow(1 - t, 2) * y1) - Math.pow(t, 2) * y2) / (2 * t * (1 - t))
					dom.setLineCap('round')
					dom.beginPath();
					dom.setStrokeStyle('black')
					dom.setLineWidth(5)
					dom.moveTo(x1, y1);
					dom.quadraticCurveTo(cx, cy, x2, y2);
					
					dom.stroke();
					dom.draw(true)
				}

			},
			clear() {
				this.dom.clearRect(0, 0, 1000, 1000)
				this.dom.draw()
			},
			save() {
				var t=this;
				uni.canvasToTempFilePath({
					canvasId: 'designature',
					fileType: 'png',
					quality: 1, //图片质量
					success:function(res) {
						//console.log(res.tempFilePath)
						t.$emit('getImg',res.tempFilePath)
					},
					fail(e){
						console.log(e)
					}
				},this)
			},
			cancel(){
				this.clear();
				this.$emit('cancelSign')
			}
		}
	}
</script>

<style scoped lang="scss">
	.signa {
		position: relative;
		overflow: hidden;
		background-color: #fbfbfb;
		height: 100%;
		width: 100%;
		.canvas {
			width: 100%;
			height: calc(100% - 160rpx);
			background-color: #FFFFFF;
			position: absolute;
			top: 80rpx;
			left: 0;
			right: 0;
			bottom: 80rpx;
			z-index: 9999;
			border: 1px solid #d6d6d6;
		}
		.btn {
			height: 80rpx;
			line-height: 80rpx;
			width: 100%;
			position: fixed;
			font-size: 32rpx;
			bottom:0;
			left:0;
			display: flex;
			z-index: 99999;
			border-top: 2rpx solid #f5f5f5;
			.cancel-btn {
				flex: 1;
				color: #333;
				text-align: center;
			}

			.save-btn {
				flex: 1;
				color: #A91F52;
				text-align: center;
			}
		}
		.head{
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 80rpx;
			line-height: 80rpx;
			display: flex;
			padding: 0 20rpx;
			
			.title{
				flex: 1;
				color: #333;
				font-size: 32rpx;
				font-weight: 700;
			}
			.cancel{
				width: 60rpx;
				color: #999;
				font-size: 28rpx;
			}
		}
	}
</style>
